//
//  ViewController.m
//  点餐系统
//
//  Created by song on 15/1/19.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource ,UIPickerViewDelegate>

@end

@implementation ViewController

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.foods[component] count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return  self.foods[component][row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            self.fruitLable.text = self.foods[component][row];
            break;
        case 1:
             self.foodLable.text = self.foods[component][row];
            break;
        case 2:
            self.drinkLable.text = self.foods[component][row];
            break;
            
        default:
            break;
    }
}


-(NSArray *)foods
{
    if(_foods ==nil)
    {
        NSArray *array =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]];
        _foods =array;
        
    }
    
    return _foods;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    // 自动选择三列
    [self pickerView:nil didSelectRow:0 inComponent:0];
    [self pickerView:nil didSelectRow:0 inComponent:1];
    [self pickerView:nil didSelectRow:0 inComponent:2];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)randomClick:(id)sender {
    
    for (int i = 0 ; i<self.foods.count; i++) {
        
        int count =(int)[self.foods[i] count];
        
        int row = arc4random()% count;
        
//         获取选择的行
//        int oldrow = (int)[self.pickview selectedRowInComponent:i];
        
        
        
        
        [self pickerView:nil didSelectRow:row inComponent:i]; //让选择器随机选择到某一列
        [self.pickview selectRow:row inComponent:i animated:YES]; // 让选择器选择到某一列，然后显示到标签里面
    }

}

@end
